package com.atguigu.service.impl;

import com.atguigu.dao.AdminRoleDao;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.RoleDao;
import com.atguigu.entity.AdminRole;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminRoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@DubboService
public class AdminRoleServiceImpl extends BaseServiceImpl<AdminRole> implements AdminRoleService {
    @Autowired
    private AdminRoleDao adminRoleDao;
    @Autowired
    private RoleDao roleDao;
    @Override
    public Map<String, List<Role>> findRoleByAdminId(Long adminId) {
        List<Role> roleList = roleDao.getAll();
        List<Long> roleIds = adminRoleDao.findRoleIdByAdminId(adminId);
        List<Role> assignRoleList = new ArrayList<>();
        List<Role> noAssignRoleList = new ArrayList<>();
        for (Role role : roleList) {
            if (roleIds.contains(role.getId())){
                assignRoleList.add(role);
            }else{
                noAssignRoleList.add(role);
            }
        }
        Map<String, List<Role>> map = new HashMap<>();
        map.put("assignRoleList", assignRoleList);
        map.put("noAssignRoleList", noAssignRoleList);
        return map;
    }

    @Override
    public void insertAdminRole(Long adminId, String[] roleIds) {
        adminRoleDao.delete(adminId);
        for (String roleId : roleIds) {
            if (roleId.equals(""))
                continue;
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(Long.parseLong(roleId));
            adminRoleDao.insert(adminRole);
        }

    }

    @Override
    public BaseDao<AdminRole> getBaseDao() {
        return adminRoleDao;
    }
}
